//
//  LYChatViewController.swift
//  WeChat
//
//  Created by mac on 2018/8/13.
//  Copyright © 2018年 RRC. All rights reserved.
//

import UIKit

class LYChatViewController: UIViewController {

    var messageModel:LYMessageModel?
    
    var itemDataSouce = [ChatModel]()
    
    lazy var listTableView:UITableView = {
        let listTableView = UITableView(frame: CGRect.zero, style: UITableViewStyle.plain)
        listTableView.dataSource = self
        listTableView.delegate = self
        listTableView.backgroundColor = UIColor.clear
        listTableView.separatorStyle = UITableViewCellSeparatorStyle.none
        listTableView.backgroundView = UIImageView(image: LYAsset.Chat_background.image)
        return listTableView
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        self.view.backgroundColor = UIColor.viewBackgroundColor
        self.navigationController?.interactivePopGestureRecognizer?.isEnabled = true
        
        self.title = self.messageModel!.nickName
        
        self.listTableView.ts_registerCellClass(LYChatTextCell.self)
        self.listTableView.ts_registerCellClass(LYChatImageCell.self)
        self.listTableView.ts_registerCellClass(LYChatVoiceCell.self)
        self.listTableView.ts_registerCellClass(LYChatSystemCell.self)
        self.listTableView.ts_registerCellClass(LYChatTimeCell.self)
        self.listTableView.tableFooterView = UIView()
        self.listTableView.estimatedRowHeight = 0
        self.listTableView.estimatedSectionHeaderHeight = 0
        self.listTableView.estimatedSectionFooterHeight = 0
        
        self.view.addSubview(listTableView)
        listTableView.snp.makeConstraints { (make) in
            make.top.left.right.bottom.equalTo(self.view)
        }
        
        
        //获取第一屏的数据
        self.firstFetchMessageList()
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}


extension LYChatViewController:UITableViewDelegate{
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
    }
}

// MARK: - UITableViewDataSource
extension LYChatViewController:UITableViewDataSource{
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.itemDataSouce.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        let chatModle = self.itemDataSouce[indexPath.row]
        let type:MessageContentType = chatModle.messageContentType
//        print(type.chatCellHeight(chatModle))
        return type.chatCellHeight(chatModle)
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if itemDataSouce.count > 0 {
            let chatModel = self.itemDataSouce[indexPath.row]
            let type:MessageContentType = chatModel.messageContentType
            return type.chatCell(tableView, indexPath: indexPath, model: chatModel, viewController: self)!
        }
        return LYChatBaseCell()
    }
    
    
}


